from starlette.config import Config
from starlette.datastructures import Secret
import os

config = Config('.env')

######################################################################################################
# database

DB_DRIVER = config('DB_DRIVER', default='postgresql')

WRITE_DB_HOST = config('WRITE_DB_HOST', default='127.0.0.1')
WRITE_DB_PORT = config('WRITE_DB_PORT', cast=int, default=5432)
WRITE_DB_USER = config('WRITE_DB_USER', default='postgres')
WRITE_DB_PASSWORD = config('WRITE_DB_PASSWORD', cast=Secret, default=None)
WRITE_DB_DATABASE = config('WRITE_DB_DATABASE', default=None)

READ_DB_HOST = config('READ_DB_HOST', default=None) or WRITE_DB_HOST
READ_DB_PORT = config('READ_DB_PORT', cast=int, default=0) or WRITE_DB_PORT
READ_DB_USER = config('READ_DB_USER', default=None) or WRITE_DB_USER
READ_DB_PASSWORD = config('READ_DB_PASSWORD', cast=Secret, default=None) or WRITE_DB_PASSWORD
READ_DB_DATABASE = config('READ_DB_DATABASE', default=None)

DB_POOL_MIN_SIZE = config('DB_POOL_MIN_SIZE', cast=int, default=1)
DB_POOL_MAX_SIZE = config('DB_POOL_MAX_SIZE', cast=int, default=16)
DB_ECHO = config('DB_ECHO', cast=bool, default=False)
DB_SSL = config('DB_SSL', default=None)
DB_USE_CONNECTION_FOR_REQUEST = config('DB_USE_CONNECTION_FOR_REQUEST', cast=bool, default=True)
DB_RETRY_LIMIT = config('DB_RETRY_LIMIT', cast=int, default=1)
DB_RETRY_INTERVAL = config('DB_RETRY_INTERVAL', cast=int, default=1)

CODE_ENV = config('CODE_ENV', default='dev')


REDIS_HOST = config('REDIS_HOST', default='127.0.0.1')
REDIS_PORT = config('REDIS_PROT', default=6379)
REDIS_DB = config('REDIS_PROT', default=0)


if CODE_ENV == 'product':
    WRITE_DB_DATABASE += '_prd'
    READ_DB_DATABASE += '_prd'
elif CODE_ENV == 'dev':
    WRITE_DB_DATABASE += '_dev'
    READ_DB_DATABASE += '_dev'
else:
    WRITE_DB_DATABASE += '_test'
    READ_DB_DATABASE += '_test'

WRITE_DSN = f'{DB_DRIVER}://{WRITE_DB_USER}:{WRITE_DB_PASSWORD}@{WRITE_DB_HOST}:{WRITE_DB_PORT}/{WRITE_DB_DATABASE}'
READ_DSN = f'{DB_DRIVER}://{READ_DB_USER}:{READ_DB_PASSWORD}@{READ_DB_HOST}:{READ_DB_PORT}/{READ_DB_DATABASE}'

BASE_DB_CONFIG = {
    'pool_min_size': DB_POOL_MIN_SIZE,
    'pool_max_size': DB_POOL_MAX_SIZE,
    'echo': DB_ECHO,
    'ssl': DB_SSL,
    'use_connection_for_request': DB_USE_CONNECTION_FOR_REQUEST,
    'retry_limit': DB_RETRY_LIMIT,
    'retry_interval': DB_RETRY_INTERVAL
}

WRITE_DB_CONFIG = {
    'dsn': WRITE_DSN,
    **BASE_DB_CONFIG
}

READ_DB_CONFIG = {
    'dsn': READ_DSN,
    **BASE_DB_CONFIG
}


# 默认缓存
Cache = {
        "host": REDIS_HOST,
        "port": REDIS_PORT,
        "db": REDIS_DB,

    }

# 验证码缓存
Code = {
        "host": REDIS_HOST,
        "port": REDIS_PORT,
        "db": 1,
        "poolName": "code",
        "decode_responses": True
}


######################################################################################################
# config

LOG_LEVEL = config('LOG_LEVEL', default='DEBUG')
LOG_PATH = config('LOG_PATH')

# jwt
ACCESS_TOKEN_EXPIRE_MINUTES = 15
SECRET_KEY = "28f8a6b7-1ac7-4c2c-b22d-263295fbeabd"
ALGORITHM = "HS256"

OBJECT_PATH = os.path.join(os.getcwd())
IMAGE_PATH = os.path.join(OBJECT_PATH, 'static')

VERSION = 'V1'
